package com.card.hwatt.assembler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;

import com.card.hwatt.core.ObjectUtil;
import com.card.hwatt.dto.PageResult;
import com.card.hwatt.entity.*;

/**
 * 请假
 * @author Administrator
 *
 */
public final class LeaveAssembler {

	public static PageResult<com.card.hwatt.dto.Leave> convert(
			PageResult<Leave> list) {
		PageResult<com.card.hwatt.dto.Leave> result = new PageResult<com.card.hwatt.dto.Leave>();
		result.total = list.total;
		result.rows = new ArrayList<com.card.hwatt.dto.Leave>();
		for (Leave item : list.rows) {
			com.card.hwatt.dto.Leave dto = convert(item);
			result.rows.add(dto);
		}
		return result;
	}

	public static List<com.card.hwatt.dto.Leave> convert(List<Leave> list) {
		List<com.card.hwatt.dto.Leave> rows = new ArrayList<com.card.hwatt.dto.Leave>();
		for (Leave item : list) {
			com.card.hwatt.dto.Leave dto = convert(item);
			rows.add(dto);
		}
		return rows;
	}

	public static com.card.hwatt.dto.Leave convert(Leave entity) {
		com.card.hwatt.dto.Leave dto = new com.card.hwatt.dto.Leave();
		ObjectUtil.fieldCopy(entity, dto);

		dto.employeeId = entity.employee.id;
		dto.employeeCode = entity.employee.employeeCode;
		dto.employeeName = entity.employee.name;

		return dto;
	}

	public static Leave convert(com.card.hwatt.dto.Leave dto) {
		Leave entity = new Leave();
		ObjectUtil.fieldCopy(dto, entity);

		entity.employee = new Employee();
		entity.employee.id = dto.employeeId;

		return entity;
	}

	public static PageResult<com.card.hwatt.dto.LeaveView> convertPage(
			PageResult<Object[]> pageResult) {

		PageResult<com.card.hwatt.dto.LeaveView> result = new PageResult<com.card.hwatt.dto.LeaveView>();
		result.rows = new ArrayList<com.card.hwatt.dto.LeaveView>();
		result.total = pageResult.total;
		for (Object[] item : pageResult.rows) {
			com.card.hwatt.dto.LeaveView dto = new com.card.hwatt.dto.LeaveView();
			if (item[0] instanceof com.card.hwatt.entity.Employee) {

				Employee entity = (Employee) item[0];
				dto.employeeId = entity.id;
				dto.employeeCode = entity.employeeCode;
				dto.employeeName = entity.name;
				dto.departmentName = entity.department.name;
			}

			if (item[1] != null) {
				dto.value = item[1].toString();
			}
			if (item[2] instanceof Date) {
				dto.date = (Date) item[2];
			}

			if (item[3] != null) {
				dto.key = item[3].toString();
			}

			if (item[4] != null) {
				dto.name = item[4].toString();
				try {
					dto.startDay = DateUtils.parseDate(item[4].toString(),
							new String[] { "yyyyMMdd" });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			if (item[5] != null) {
				try {
					dto.endDay = DateUtils.parseDate(item[5].toString(),
							new String[] { "yyyyMMdd" });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			dto.day = Integer.valueOf(item[6].toString());

			if (item[7] != null) {
				dto.reason = item[7].toString();
			}

			result.rows.add(dto);
		}

		return result;
	}
}
